package com.qujia.station.service.impl;

import com.qujia.common.constant.UserConstants;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.exception.BaseException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.StringUtils;
import com.qujia.station.domain.Commodity;
import com.qujia.station.domain.Supplier;
import com.qujia.station.mapper.SupplierMapper;
import com.qujia.station.service.CommodityService;
import com.qujia.station.service.SupplierService;
import com.qujia.station.utils.CodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service("supplierService")
@Slf4j
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    @Autowired
    private CommodityService commodityService;


    @Override
    public int saveSupplier(Supplier supplier) {
        int i = 0;
        synchronized (this){
            String code = null;
            boolean flag = true;
            // 校验 重复
            while (flag) {
                code = CodeUtil.createCode();
                Supplier result = supplierMapper.selectBySupplierCode(code);
                if(Objects.isNull(result)){
                    flag = false; // 终止循环
                }
            }
            supplier.setSupplierCode(code);
            i = supplierMapper.insertSelective(supplier);
        }
        return i;
    }

    @Override
    public List<Supplier> selectSupplierList(Supplier supplier) {
        return supplierMapper.selectSupplierList(supplier);
    }

    @Override
    public Supplier selectOneSupplier(Supplier supplier) {
        return supplierMapper.selectOneSupplier(supplier);
    }

    @Override
    public int updateSupplier(Supplier supplier) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getUser();
        supplier.setUpdateUser(sysUser.getUserId());
        supplier.setUpdateTime(new Date());
        return supplierMapper.updateByPrimaryKeySelective(supplier);
    }

    @Override
    public int deleteSupplier(List<Long> supplierIdList) {
        return supplierMapper.deleteSupplier(supplierIdList);
    }

    @Override
    public List<Supplier> selectSupplierAllList() {
        return supplierMapper.selectSupplierAllList();
    }

    @Override
    public Supplier getSupplierIdList(Supplier supplier) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getUser();
        if(Objects.isNull(sysUser)){
            throw new BaseException("该账号不存在,请重新登录");
        }
        if(Objects.nonNull(supplier.getSupplierId()) || StringUtils.isNotEmpty(supplier.getSupplierCode())){
            return supplier;
        }
        if(StringUtils.isEmpty(sysUser.getUserLevel()) || UserConstants.LEVEL_ONE.equals(sysUser.getUserLevel())){
            return supplier;
        }
        if(UserConstants.LEVEL_TWO.equals(sysUser.getUserLevel())){
            Commodity commodity = new Commodity();
            commodity.setEnterpriseId(sysUser.getEnterpriseId());
            List<Long> supplierIdList = commodityService.getSupplierIdList(commodity);
            supplier.setSupplierIdList(supplierIdList);
        } else if(UserConstants.LEVEL_THREE.equals(sysUser.getUserLevel())){
            Commodity commodity = new Commodity();
            commodity.setStationId(sysUser.getStationId());
            List<Long> supplierIdList = commodityService.getSupplierIdList(commodity);
            supplier.setSupplierIdList(supplierIdList);
        }
        return supplier;
    }


}
